import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Flutter_to_Android extends StatelessWidget {
  const Flutter_to_Android({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("Android原生与Flutter交互",textDirection: TextDirection.ltr),
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          padding: EdgeInsets.all(10),
          child: ListView(
            children: <Widget>[
              ToastWidget(),
              GetAndroidDataWidget(),
              JumpAndroidActivity()
            ],
          ),
        ),
      ),
    );
  }
}
class ToastWidget extends StatelessWidget {
  const ToastWidget({Key? key}) : super(key: key);
  static const _platform = const MethodChannel("com.example.flutter_demo/getApp");

  void _getToast(){
    Map<String, String> map = { "flutter": "这是一条来自flutter的参数" };
    _platform.invokeMethod("Toast",map);
    print("toast按钮点击");
  }
  @override
  Widget build(BuildContext context) {
    return  TextButton(onPressed: _getToast, child: Text("调用Android原生Toast,自定义toast内容",style: TextStyle(
        color: Colors.white
    ),),
      style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(Colors.black45),
          padding:MaterialStateProperty.all(EdgeInsets.fromLTRB(0, 10, 0, 10))
      ),);
  }
}
 class GetAndroidDataWidget extends StatefulWidget {
   const GetAndroidDataWidget({Key? key}) : super(key: key);
   @override
   _GetAndroidDataWidgetState createState() => _GetAndroidDataWidgetState();
 }
 
 class _GetAndroidDataWidgetState extends State<GetAndroidDataWidget> {
   static const _platform = const MethodChannel("com.example.flutter_demo/getApp");
   Future<void>  _getData() async{
      String result = await _platform.invokeMethod('Data');
     print("获取来的数据：="+result);
   }
   @override
   Widget build(BuildContext context) {
     return  TextButton(onPressed: _getData, child: Text("获取Android中的数据，并打印",style: TextStyle(
         color: Colors.white
     ),),
       style: ButtonStyle(
           backgroundColor: MaterialStateProperty.all(Colors.black45),
           padding:MaterialStateProperty.all(EdgeInsets.fromLTRB(0, 10, 0, 10))
       ),);
   }
 }
 class JumpAndroidActivity extends StatelessWidget {
   const JumpAndroidActivity({Key? key}) : super(key: key);
   static const _platform = const MethodChannel("com.example.flutter_demo/getApp");
   void _jumpAndroidActivity(){
     print("测试数据");
     Map<String, String> map = { "flutter": "跳转Activity传递数据" };
     _platform.invokeMethod("JumpActivity",map);
   }
   @override
   Widget build(BuildContext context) {
     return TextButton(onPressed: _jumpAndroidActivity, child: Text("跳转Android的Activity并传值",style: TextStyle(
         color: Colors.white
     ),),
       style: ButtonStyle(
           backgroundColor: MaterialStateProperty.all(Colors.black45),
           padding:MaterialStateProperty.all(EdgeInsets.fromLTRB(0, 10, 0, 10))
       ),);
   }
 }

 
